/**
 * The javascript for competence operation.
 */
dojo.require("dojox.grid.DataGrid");

var Level = function(value, name, desc) {
	this.value = value;
	this.name = name;
	this.desc = desc;
};

var levels = [ new Level(1, "菜鸟", "刚刚开始学习写Hello World,在项目中使用时还需别人帮助或经常Google"),
		new Level(2, "入门", "了解常用的API,能独立的在项目中使用,不需要或较少需要别人帮助"),
		new Level(3, "熟练", "熟练使用常用的API并在多个项目中应用,并能帮助别人"),
		new Level(4, "精通", "能应用自如并能总结自己的观点"),
		new Level(5, "专家", "主导过大型开源项目或出版专著或对该技术发展作出积极贡献") ];

function nextLevel() {
	var currentLevel = dojo.byId("competence.level");
	var nextLevel = new Number(currentLevel.value) + 1;
	if (nextLevel > 5) {
		nextLevel = 1;
	}
	var level = levels[nextLevel - 1];
	var levelValue = dojo.byId("levelValue");
	var levelDesc = dojo.byId("levelDesc");
	levelValue.innerHTML = level.value;
	levelDesc.innerHTML = level.name + " - " + level.desc;
	currentLevel.value = level.value;
}

function initLevel() {
	var level = levels[0];
	var levelValue = dojo.byId("levelValue");
	var levelDesc = dojo.byId("levelDesc");
	var currentLevel = dojo.byId("competence.level");
	levelValue.innerHTML = level.value;
	levelDesc.innerHTML = level.name + " - " + level.desc;
	currentLevel.value = level.value;
}

function saveCompetence() {
	var form = dojo.byId("createCompetenceForm");
	var xhrArgs = {
		form : dojo.byId("createCompetenceForm"),
		handleAs : "json",
		load : function(data) {
			if(data.competence.saved=="true"){
				loadCompetences();
			}
		},
		error : function(error) {
			dojo.byId("myCompetences").innerHTML = "Form posted.";
		}
	};
	var deferred = dojo.xhrPost(xhrArgs);
}

function loadCompetences() {
	var url = "/my/competences";
	// Look up the node we'll stick the text under.
	var targetNode = dojo.byId("myCompetences");

	// The parameters to pass to xhrGet, the url, how to handle it, and the
	// callbacks.
	var xhrArgs = {
		url : url,
		handleAs : "json",
		load : function(data) {
			for(var i=0;i<data.competences.length;i++) {
				targetNode.innerHTML +="<span>"+data.competences[i].item+" - "+data.competences[i].level+"</span></br/>";
			}
		},
		error : function(error) {
			targetNode.innerHTML = "An unexpected error occurred: " + error;
		}
	}

	// Call the asynchronous xhrGet
	var deferred = dojo.xhrGet(xhrArgs);
}

dojo.addOnLoad(initLevel);
dojo.addOnLoad(loadCompetences);